<?php
/**
 *  date: 2017/7/10  11:03
 *  Author: Lin
 *  encoding:utf-8  language:PHP
 */
namespace Admin\Controller;
use Admin\Model\AnswerModel;

class AnswerController extends AdminBaseController
{
    protected $_answerObject;

    public function _initialize()
    {
        parent::_initialize();
        $this->answerObject = new AnswerModel;
    }

    /**
     *学习题文
     * @Date 201707
     * @Author Lin
     */
    public function learnEdit()
    {
        $lqModel = M('learn_question');
        if(IS_POST)
        {
            if($lqModel->create(I('post.'),2))
            {
                if(false !== $lqModel->save())
                {
                    $this->success('修改成功');
                    exit;
                }
            }
            $this->error($lqModel->getError());
        }
        $data = $lqModel->find();
        $this->assign('data',$data);
        $this->display();
    }

    /**
     *题库列表
     * @Date 201707
     * @Author Lin
     */
    public function questionList()
    {
        $data = $this->answerObject->getQuestionList(I('title'));

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();
    }

    /**
     *增加题目
     * @Date 201707
     * @Author Lin
     */
    public function addQuestion()
    {
        $model = M('question_bank');
        if (IS_POST)
        {
            if ($model->create(I('post.'), 1))
            {
                $data = I('post.');
                $data['exactness'] = strtolower($data['exactness']);
                if ($model->add($data))
                {
                    $this->success('添加成功', U('questionList'), 2);
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $this -> display();
    }


    /**
     * 题库修改
     */
    public function questionEdit()
    {
        $id=I('id');
        $model = M('question_bank');
        if (IS_POST)
        {
            if ($data = $model->create(I('post.'),2))
            {
                $data = I('post.');
                $data['exactness'] = strtolower($data['exactness']);
                if (FALSE !== $model->save($data))
                {
                    $this->success('修改成功','questionList',1);
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $data = $model ->where(array('id'=>$id)) ->find();
        $this -> assign('data', $data);
        $this -> display();
    }


    /**
     * 题库删除
     */
    public function questionDel()
    {
        if(!IS_AJAX) return;
        $result = M('question_bank')-> delete(intval($_GET['id']));
        if($result)
            $this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
        else
            $this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
    }

    /**
     *全选删除
     * @Date 201703
     * @Author Linsenquan
     */
    public function deleteAllQuestion()
    {
        if(!IS_POST)
            $this->error('非法操作');
        if(empty($_POST))
            $this->error('请选择要删除的信息！','',2);
        //var_dump($_POST);die;
        $result = $this->answerObject->deleteAllQuestion($_POST);

        if($result)
            $this->success('删除成功',U('questionList'),2);
        else
            $this->error('删除失败','',2);
    }

    /**
     *设置答题数
     * @Date 201707
     * @Author Lin
     */
    public function answerNum()
    {
        $numModel = M('answer_num');
        if(IS_POST)
        {
            //var_dump($_POST);die;
            if($numModel->create(I('post.'),2))
            {
                if(false !== $numModel->save())
                {
                    $this->success('修改成功');
                    exit;
                }
            }
            $this->error($numModel->getError());
        }
        $data = $numModel->find();
        $this->assign('data',$data);
        $this->display();
    }

    /**
     *答题列表
     * @Date 201707
     * @Author Lin
     */
    public function answerInfoList()
    {
        $username = I('username');
        $school = I('school');
        $area = I('area');
        $city = I('city');
        if($city)
        {
            $_city = explode('_',$city);
            $city =$_city[0];
            $areadata = M('Tree')->where (array('pid'=>$_city[1]) )->select();//处理第一次只选市搜索后再点区没有区/县数据返回的情况和搜索后选中状态
        }
       //var_dump($area);
        $data = $this->answerObject->getAnswerInfoList($username,$school,$area,$city);
        $province = M('Tree')->where (array('pid'=>6) )->select();//广东省

        $this->assign('province',$province);
        $this->assign('city',$_city[1]);
        $this->assign('area',$area);
        $this->assign('areadata',$areadata);

        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->assign('p', I('get.p', 1));
        $this->assign('perCount', 10);
        $this->display();
    }

    /**
     * 答题删除
     */
    public function answerInfoDel()
    {
        if(!IS_AJAX) return;
        $result = $this->answerObject-> delete(intval($_GET['id']));
        if($result)
            $this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
        else
            $this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
    }

    /**
     * 信息修改
     */
    public function answerInfoEdit()
    {
        $id=I('id');
        $model = $this->answerObject;
        if (IS_POST)
        {
            if ($data = $model->create(I('post.'),2))
            {
                if (FALSE !== $model->save())
                {
                    $this->success('修改成功','answerInfoList',2);
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $data = $model ->alias('info')->field('info.*,u.nickname')->where(array('info.id'=>$id))->join('LEFT JOIN __USERS__ u ON u.id=info.user_id')->find();
        $this -> assign('data', $data);
        $this -> display();
    }

    /**
     *全选删除
     * @Date 201703
     * @Author Linsenquan
     */
    public function deleteAllInfo()
    {
        //var_dump($_POST);die;
        if(!IS_POST)
            $this->error('非法操作');
        if(empty($_POST))
            $this->error('请选择要删除的信息！','',2);

        $result = $this->answerObject->deleteAllInfo($_POST);
        if($result)
            $this->success('删除成功',U('answerInfoList'),2);
        else
            $this->error('删除失败','',2);
    }


    public function getRegion(){
        $Region=M("tree");
        $map['pid']=$_REQUEST["pid"];
        $map['type']=$_REQUEST["type"];

        $list=$Region->where($map)->select();

        //var_dump($list);
        echo json_encode($list);
    }


    /**
     *状态展示
     * @Date 201707
     * @Author Lin
     */
    public function statusEdit()
    {
        $Id = I('Id');
        $status = I('status');
        //var_dump($menuId);var_dump($status);die;

        $result = M('question_bank')->where(array('id'=>$Id))->setField('is_display',$status);

        if ($result !== false)
            $this->ajaxReturn(array('status'=>1));
        else
            $this->ajaxReturn(array('status'=>0));
    }


}